<?php
/**
 * Created by PhpStorm.
 * User: lx
 * Date: 2020/1/6
 * Time: 15:57
 */
namespace app\mtadminapi\controller;

use app\command\controller\Common;
use app\mtadminapi\controller\Base;
use app\mtadminapi\model\MtAdminSysShopExam;
use app\mtadminapi\model\MtShop;
use app\util\ReturnCode;
use think\Validate;

/**
 * 后台管理系统商户管理
 * Class AdminSysShopHandleAdmin
 * @package app\mtadminapi\controller
 */
class AdminSysShopHandle extends Base
{
//    public function __construct(){
//        parent::__construct();
//    }
    public $rule = [];
    public $msg = [];
    public $checkData = [];

    public function index(){
        print_r($this->getToken());die;
        $this->request->path();
        print_r(date("H:i"));die;
        print_r((new MtAdminSysShopExam())->insert("aabbcc"));exit();
    }

    /**
     * 门店信息设置
     * @todo 参数注释
     */
    public function storeMessageSet(){
        //验证规则
        $this->rule = [
            'applyAdminID'   => 'require|number',
            'shopEmail' => 'email',
            'shopTel' => 'require|mobile',
            'shopTel2' => 'mobile',
            'shopAdress' => 'require',
            'shopAdressInfo' => 'require',
            'shopLabel' => 'require',
            'shopLogo' => 'require',  // @todo
            'shopName' => 'require|max:20',
            'landlordNick' => 'require|max:20',
            'houseNum'  =>  'number',
            'adressProvinceID' => 'number',
            'adressCityID' => 'number',
            'adressAreaID' => 'number',
            'content'   =>  'max:180',
            'welcome'   =>  'max:50',
            ];
        //提示信息
        $this->msg = [
            'applyAdminID.require' => 'adminid不能为空',
            'applyAdminID.number'   => 'id需要是数字',
            'shopTel.require'     => '电话必填',
            'shopTel.mobile'  => '请填写正确的电话号',
            'shopEmail'        => '邮箱格式不正确',
            'shopTel2'        => '请填写正确的备用电话号',
            'shopAdress'        => '地址必填',
            'shopAdressInfo'        => '地址必填',
            'shopLabel'        => '请选择标签',
            'shopName.require'        => '商店名必填',
            'shopName.max'        => '商店名长度不能超过20',
            'landlordNick.require'        => '请填写店主昵称',
            'landlordNick.max'  => '昵称长度不能超过20',
            'shopLogo'      => '请上传logo',
            'houseNum'      =>  '房间数量为数字',
            'adressProvinceID' => '省ID',
            'adressCityID' => '市ID',
            'adressAreaID' => '区ID',
            'content'   =>  '简介不能超过180字',
            'welcome'   =>  '欢迎语不能超过五十字',
            ];
        $adminID = $this->request->param("adminID"); // 管理员ID
        if(!$adminID){
            return $this->bindError(ReturnCode::EMPTY_PARAMS,"adminID不存在");
        }
        $shopLogo = $this->request->post("shopLogo");   //商家logo
        $shopName = $this->request->post("shopName");   // 商家名称
        $landlordNick = $this->request->post("landlordNick");   // 店主昵称
        $shopTel = $this->request->post("shopTel");     // 商家联系电话
        $shopTel2 = $this->request->post("shopTel2");   // 商家备用联系电话
        $shopAdress = $this->request->post("shopAdress");   // 商家地址
        $shopAdressInfo = $this->request->post("shopAdressInfo");    // 商家详细地址
        $shopLabel = $this->request->post("shopLabel");     // 商家标签
        $adressProvinceID = $this->request->post("adressProvinceID");   // 省
        $adressCityID = $this->request->post("adressCityID");   // 市
        $adressAreaID = $this->request->post("adressAreaID");   //区
        $welcome = $this->request->post("welcome");     //欢迎语
        $shopEmail = $this->request->post("shopEmail");     //邮箱
        $content = $this->request->post("content");     //商家介绍
        $houseNum = $this->request->post("houseNum");   //房间数量

        $this->checkData = [
            "applyAdminID"      => $adminID,
            "shopLogo"          => $shopLogo,
            "shopName"          => $shopName,
            "landlordNick"      => $landlordNick,
            "shopTel"           => $shopTel,
            "shopTel2"          => isset($shopTel2) ? $shopTel2 : "",
            "shopAdress"        => $shopAdress,
            "shopAdressInfo"    => $shopAdressInfo,
            "shopLabel"         => $shopLabel,
            "adressProvinceID"  => $adressProvinceID,
            "adressCityID"      => $adressCityID,
            "adressAreaID"      => $adressAreaID,
            "welcome"            => isset($welcome) ? $welcome : "",
            "shopEmail"         => isset($shopEmail) ? $shopEmail : "",
            "content"           => isset($content) ? $content : "",
            "houseNum"          => isset($houseNum) ? $houseNum : "",
        ];
        $result = (new Common())->VerifiString($this->rule,$this->msg,$this->checkData);
        if($result['code'] == 2){
            return $this->bindError(ReturnCode::PARAM_INVALID,$result['msg']);
        }
        $insertRes = (new MtAdminSysShopExam())->insert($this->checkData,true);
        if($insertRes == 2){
            return $this->bindError(ReturnCode::ADD_REPEAT,"重复记录");
        }elseif ($insertRes == 1){
            return $this->bindSuccess("添加成功");
        }else{
            return $this->bindError(ReturnCode::ADD_FAILED,"入库失败");
        }
    }

    /**
     * 商户门店信息修改
     */
    public function storeMessageUpdate(){
        $insertData = $this->request->param();

    }
    /**
     * 商家logo上传
     * @return mixed|void
     */
    public function shopLogoUpload(){
        if(!$this->request->file("shopLogo")){
           return $this->bindError(ReturnCode::EMPTY_PARAMS,"文件不存在");
        }
        $shopLogo = (new Common())->upload(config("apiadmin.ADMIN_SHOP_LOGO"),"shopLogo");  // 商家logo
        return $shopLogo;   // $todo
    }
}